/*
 *  GWReverserEffect.cpp
 *  GWGlitch
 *
 *  Created by Michael Forrest on 01/03/2008.
 *  Copyright 2008 Grimaceworks. All rights reserved.
 *
 */

#include "GWReverserEffect.h"
void GWReverserEffect::Process(const Float32		*inSourceP,
								Float32		*inDestP,  
								UInt32 		inFramesToProcess,  
								UInt32		inNumChannels,  
								bool		&ioSilence){
	UInt32 nSampleFrames = inFramesToProcess;
	const Float32 *sourceP = inSourceP;
	Float32 *destP = inDestP;
	Float32 beats = kernel->GetParameter(kParam_ReverserBeats);
	Float32 oneBeat = (60 * sampleRate) / bpm; 
	Float32 loopLength = oneBeat * beats;
	while (nSampleFrames-- > 0) {
		Float32 inputSample = *sourceP;
		if(forwards){
			if(loopPosition < loopLength){
				*destP = inputSample;
				loop[loopPosition] = inputSample;
				loopPosition ++;
			}
			else{
				//*destP = loop[loopPosition];
				forwards = false;
			}
		}else{
			if(loopPosition > 0){
				*destP = loop[loopPosition];
				loopPosition --;
			}else{
				*destP = inputSample;
				forwards = true;
			}
		}
		sourceP += inNumChannels;
		destP += inNumChannels;
	}

}

